// Package store
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2024 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
// @AutoGenerate Version 2.15.7
package store

import (
	"context"
	"hotgo/internal/consts"
	"hotgo/internal/model/entity"
	"hotgo/internal/model/input/form"
	"hotgo/utility/validate"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gmeta"
)

// StoreProductUpdateFields 修改商品字段过滤
type StoreProductUpdateFields struct {
	Title        string `json:"title"        dc:"商品标题"`
	Subtitle     string `json:"subtitle"     dc:"商品子标题"`
	Code         string `json:"code"         dc:"商品编码"`
	ShareContent string `json:"shareContent" dc:"分享文案"`
	Image        string `json:"image"        dc:"商品主图"`
	Price        int    `json:"price"        dc:"售价（分）"`
	Stock        int    `json:"stock"        dc:"库存"`
	Limit        int    `json:"limit"        dc:"是否限购"`
	FakeSale     int    `json:"fakeSale"     dc:"虚拟销量"`
	Sale         int    `json:"sale"         dc:"真实销量"`
	IsHot        int    `json:"isHot"        dc:"是否热门"`
	Status       int    `json:"status"       dc:"上架状态"`
	Sort         int    `json:"sort"         dc:"排序"`
}

// StoreProductInsertFields 新增商品字段过滤
type StoreProductInsertFields struct {
	Title        string `json:"title"        dc:"商品标题"`
	Subtitle     string `json:"subtitle"     dc:"商品子标题"`
	Code         string `json:"code"         dc:"商品编码"`
	ShareContent string `json:"shareContent" dc:"分享文案"`
	Image        string `json:"image"        dc:"商品主图"`
	Price        int    `json:"price"        dc:"售价（分）"`
	Stock        int    `json:"stock"        dc:"库存"`
	Limit        int    `json:"limit"        dc:"是否限购"`
	FakeSale     int    `json:"fakeSale"     dc:"虚拟销量"`
	Sale         int    `json:"sale"         dc:"真实销量"`
	IsHot        int    `json:"isHot"        dc:"是否热门"`
	Status       int    `json:"status"       dc:"上架状态"`
	Sort         int    `json:"sort"         dc:"排序"`
}

// StoreProductEditInp 修改/新增商品
type StoreProductEditInp struct {
	entity.StoreProduct
	Skus    []*StoreProductSku   `json:"skus"      dc:"规格列表"`
	Attrs   []*StoreProductAttr  `json:"attrs"      dc:"属性列表"`
	Images  []*StoreProductImage `json:"images"      dc:"商品图片列表"`
	Content string               `json:"content"      dc:"商品详情"`
}

func (in *StoreProductEditInp) Filter(ctx context.Context) (err error) {
	// 验证商品标题
	if err := g.Validator().Rules("required").Data(in.Title).Messages("商品标题不能为空").Run(ctx); err != nil {
		return err.Current()
	}

	// 验证商品编码
	if err := g.Validator().Rules("required").Data(in.Code).Messages("商品编码不能为空").Run(ctx); err != nil {
		return err.Current()
	}

	// 验证商品主图
	if err := g.Validator().Rules("required").Data(in.Image).Messages("商品主图不能为空").Run(ctx); err != nil {
		return err.Current()
	}

	// 验证售价（分）
	if err := g.Validator().Rules("regex:(^[0-9]{1,10}$)|(^[0-9]{1,10}[\\.]{1}[0-9]{1,2}$)").Data(in.Price).Messages("售价（分）最多允许输入10位整数及2位小数").Run(ctx); err != nil {
		return err.Current()
	}

	// 验证库存
	if err := g.Validator().Rules("required").Data(in.Stock).Messages("库存不能为空").Run(ctx); err != nil {
		return err.Current()
	}

	// 验证是否限购
	if err := g.Validator().Rules("required").Data(in.Limit).Messages("是否限购不能为空").Run(ctx); err != nil {
		return err.Current()
	}
	if err := g.Validator().Rules("in:1,2").Data(in.Limit).Messages("是否限购值不正确").Run(ctx); err != nil {
		return err.Current()
	}

	// 验证虚拟销量
	if err := g.Validator().Rules("required").Data(in.FakeSale).Messages("虚拟销量不能为空").Run(ctx); err != nil {
		return err.Current()
	}

	// 验证真实销量
	if err := g.Validator().Rules("required").Data(in.Sale).Messages("真实销量不能为空").Run(ctx); err != nil {
		return err.Current()
	}

	// 验证是否热门
	if err := g.Validator().Rules("required").Data(in.IsHot).Messages("是否热门不能为空").Run(ctx); err != nil {
		return err.Current()
	}

	// 验证上架状态
	if err := g.Validator().Rules("required").Data(in.Status).Messages("上架状态不能为空").Run(ctx); err != nil {
		return err.Current()
	}

	// 验证排序
	if err := g.Validator().Rules("required").Data(in.Sort).Messages("排序不能为空").Run(ctx); err != nil {
		return err.Current()
	}

	return
}

type StoreProductEditModel struct{}

// StoreProductDeleteInp 删除商品
type StoreProductDeleteInp struct {
	Id interface{} `json:"id" v:"required#主键id不能为空" dc:"主键id"`
}

func (in *StoreProductDeleteInp) Filter(ctx context.Context) (err error) {
	return
}

type StoreProductDeleteModel struct{}

// StoreProductViewInp 获取指定商品信息
type StoreProductViewInp struct {
	Id int `json:"id" v:"required#主键id不能为空" dc:"主键id"`
}

func (in *StoreProductViewInp) Filter(ctx context.Context) (err error) {
	return
}

type StoreProductAttr struct {
	gmeta.Meta `orm:"table:hg_store_product_attr"`
	Id         int    `json:"id"     dc:"主键id"`
	ProductId  int64  `json:"product_id" dc:"商品id"`
	Name       string `json:"name"     dc:"属性名称"`
	Value      string `json:"value"    dc:"属性值"`
}
type StoreProductContent struct {
	gmeta.Meta `orm:"table:hg_store_product_content"`
	Id         int    `json:"id"     dc:"主键id"`
	ProductId  int64  `json:"product_id" dc:"商品id"`
	Content    string `json:"content"    dc:"图文"`
}
type StoreProductImage struct {
	gmeta.Meta `orm:"table:hg_store_product_image"`
	Id         int    `json:"id"     dc:"主键id"`
	ProductId  int64  `json:"product_id" dc:"商品id"`
	Image      string `json:"image"    dc:"图片"`
}
type StoreProductSku struct {
	gmeta.Meta `orm:"table:hg_store_product_sku"`
	Id         int    `json:"id"     dc:"主键id"`
	ProductId  int64  `json:"product_id" dc:"商品id"`
	Name       string `json:"name"     dc:"规格名称"`
	Price      string `json:"price"    dc:"价格"`
	Stock      string `json:"stock"    dc:"库存"`
	Code       string `json:"code"    dc:"编码"`
}
type StoreProductViewModel struct {
	entity.StoreProduct
	Skus    []*StoreProductSku   `json:"skus" dc:"规格列表" orm:"with:product_id=id"`
	Images  []*StoreProductImage `json:"images" dc:"图片列表" orm:"with:product_id=id"`
	Attrs   []*StoreProductAttr  `json:"attrs" dc:"属性列表" orm:"with:product_id=id"`
	Content *StoreProductContent `json:"content" dc:"商品详情" orm:"with:product_id=id"`
}

// StoreProductListInp 获取商品列表
type StoreProductListInp struct {
	form.PageReq
	Title  string `json:"title"  dc:"商品标题"`
	IsHot  int    `json:"isHot"  dc:"是否热门"`
	Status int    `json:"status" dc:"上架状态"`
}

func (in *StoreProductListInp) Filter(ctx context.Context) (err error) {
	return
}

type StoreProductListModel struct {
	Id     int    `json:"id"     dc:"主键id"`
	Title  string `json:"title"  dc:"商品标题"`
	Code   string `json:"code"   dc:"商品编码"`
	Image  string `json:"image"  dc:"商品主图"`
	Price  int    `json:"price"  dc:"售价（分）"`
	Stock  int    `json:"stock"  dc:"库存"`
	Sale   int    `json:"sale"   dc:"真实销量"`
	IsHot  int    `json:"isHot"  dc:"是否热门"`
	Status int    `json:"status" dc:"上架状态"`
	Sort   int    `json:"sort"   dc:"排序"`
}

// StoreProductExportModel 导出商品
type StoreProductExportModel struct {
	Id    int    `json:"id"    dc:"主键id"`
	Title string `json:"title" dc:"商品标题"`
	Code  string `json:"code"  dc:"商品编码"`
	Image string `json:"image" dc:"商品主图"`
	Price int    `json:"price" dc:"售价（分）"`
	Stock int    `json:"stock" dc:"库存"`
	Sale  int    `json:"sale"  dc:"真实销量"`
}

// StoreProductMaxSortInp 获取商品最大排序
type StoreProductMaxSortInp struct{}

func (in *StoreProductMaxSortInp) Filter(ctx context.Context) (err error) {
	return
}

type StoreProductMaxSortModel struct {
	Sort int `json:"sort"  description:"排序"`
}

// StoreProductStatusInp 更新商品状态
type StoreProductStatusInp struct {
	Id     int `json:"id" v:"required#主键id不能为空" dc:"主键id"`
	Status int `json:"status" dc:"状态"`
}

func (in *StoreProductStatusInp) Filter(ctx context.Context) (err error) {
	if in.Id <= 0 {
		err = gerror.New("主键id不能为空")
		return
	}

	if in.Status <= 0 {
		err = gerror.New("状态不能为空")
		return
	}

	if !validate.InSlice(consts.StatusSlice, in.Status) {
		err = gerror.New("状态不正确")
		return
	}
	return
}

type StoreProductStatusModel struct{}

// StoreProductSwitchInp 更新商品开关状态
type StoreProductSwitchInp struct {
	form.SwitchReq
	Id int `json:"id" v:"required#主键id不能为空" dc:"主键id"`
}

func (in *StoreProductSwitchInp) Filter(ctx context.Context) (err error) {
	return
}

type StoreProductSwitchModel struct{}
